layui.use(['form', 'layer', 'laydate', 'element'], function() {
	var form = layui.form();
	var $ = layui.jquery;
	var laydate = layui.laydate;
	var element = layui.element();

	//触发事件
	var active = {
		tabChange: function() {
			//切换到指定Tab项
			var deviceNo = $("#deviceNo").val();
			
			$.ajax({
				url : url + '/ic-iot/users/'+adminId+'/devices/deviceNo',
				type : 'POST',
				data:{
					deviceNo:deviceNo,
				},
				dataType : 'json',
				beforeSend: function(request) {
		            request.setRequestHeader("accessToken", accessToken);
		        },
				success : function(data) {
					if(data['code'] == 1){
						$("#device").val($("#deviceNo").val());
						$("#devices").val($("#deviceNo").val());
						element.tabChange('demo', '22'); //切换到：输入详情
						$.ajax({
							type:"get",
							url:url+'/ic-iot/users/'+adminId+'/devices/scenesAll',
							dataType : 'json',
							beforeSend: function(request) {
					            request.setRequestHeader("accessToken", accessToken);
					        },
					        success : function(data) {
					        	var htmlList = ''; 
					        	for(var i=0;i<data.result.length;i++){
					        		htmlList +='<option value='+data.result[i].sceneId+'>'+data.result[i].sceneName+'</option>';
								};
								$("#sceneId").html(htmlList);
								form.render('select');
					        }
						});
					}else{
						ifajax(data);
					}
				}
			});
		}
	};

	$('.site-demo-active').on('click', function() {
		var othis = $(this),
			type = othis.data('type');
		active[type] ? active[type].call(this, othis) : '';
	});
	
	var active1 = {
		tabChange1: function() {
			//切换到指定Tab项
			element.tabChange('demo', '11'); //切换到：绑定编号
		}
	};
	$('.layui-btn-primary').on('click', function() {
		var othis = $(this),
			type = othis.data('type');
		active1[type] ? active1[type].call(this, othis) : '';
	});
	
	var active2 = {
		tabChange2: function() {
			//切换到指定Tab项
			
			var data = formToJson($("#form-add"));
			console.log(data);
			$.ajax({
				url : url + '/ic-iot/users/'+adminId+'/devices',
				type : 'POST',
				data:data,
				dataType : 'json',
				contentType: "application/json; charset=utf-8",
				beforeSend: function(request) {
		            request.setRequestHeader("accessToken", accessToken);
		        },
				success : function(data) {
					if(data['code'] == 1){
						element.tabChange('demo', '33');
					}else{
						ifajax(data);
					}
				}
			})
			
		}
	};
	$('.site-demo-active').on('click', function() {
		var othis = $(this),
			type = othis.data('type');
		active2[type] ? active2[type].call(this, othis) : '';
	});
	
	$('#device_add').on('click', function() {
		var data = formToJson($("#form-adds"));
			$.ajax({
				url : url + '/ic-iot/setting',
					type : 'POST',
					data:data,
					dataType : 'json',
					contentType: "application/json; charset=utf-8",
					beforeSend: function(request) {
			            request.setRequestHeader("accessToken", accessToken);
			        },
					success : function(data) {
						
						if(data['code'] == 1){
							layer.msg(data['description']);
							setTimeout(function(){
						    	window.parent.location.reload();
						    },2000)
						}else{
							ifajax(data);
						}
					}
			})
	});

	/*添加地图*/
  
  
   $('#map').on('click', function() {
    var username = '选择地址';
    var href = 'map.html';
    var index = layer.open({
    	type:2,
    	title:username,
    	fixed:false,
    	maxmin:false,
    	area:['100%','100%'],
    	content:href,
    });
    layer.full(index);
  });
  
  
});
					